<?php

error_reporting(E_ALL);
ini_set("display_errors", 1);
ignore_user_abort(true);
setlocale(LC_ALL, "ru_RU.UTF-8", "rus_rus");
mb_internal_encoding("UTF-8");
mb_regex_encoding("UTF-8");
date_default_timezone_set("Europe/Moscow");

ob_start("finalize");

require_once(PATH_INTCMF . "/auxil.php");
require_once(PATH_INTCMF . "/error.php");
require_once(PATH_INTCMF . "/xnode.php");
require_once(PATH_INTCMF . "/xdom.php");

$debug_ips = array("127.0.0.1");
$error = new error(PATH_OPENID, PATH_EXAMPLE, PATH_INTCMF . "/error.xslt", "UTF-8", $debug_ips);

function finalize($output)
{
	global $error;
	//return $output . ($error->is_error_occured() ? "1" : "0");
	if ($error->is_error_occured())
	{
		// logging
		if ($_SERVER['REMOTE_ADDR'] != "127.0.0.1")
		{
			$date_suffix = date("Y-m-d_H-i-s");
			$file = PATH_LOGS . "/errors/error_{$date_suffix}.html";
			file_put_contents($file, $error->get_errors_html());
		}
		// response
		header("HTTP/1.1 500 Internal Server Error");
		header("Content-Type: text/html; charset=UTF-8");
		if ($error->is_dev_mode())
		{
			return $error->get_errors_html();
		}
		else
		{
			return "<h1>Error 500</h1><p>We are sorry, but server error was occured.</p>" . str_repeat("<!-- IE fix -->", 50);
		}
	}
	return $output;
}

?>